﻿using DAL;
using DAL.Entities;
using DAL.JSGN;
using DAL.RYGL;
using DAL.Unit;
using DAL.YQZS;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BLL.YQZS
{
    public class YqzsBLL
    {
        private readonly DataContext _dataContext;
        private readonly EmployeeDAL _employeeDAL;
        private readonly YqzsDAL _yqzsDAL;
        private readonly TrackDAL _trackDAL;
        private readonly AttachmentDAL _attachmentDAL;
        private readonly UnitDAL _unitDAL;

        public YqzsBLL()
        {
            _dataContext = new DataContext();
            _employeeDAL = new EmployeeDAL(_dataContext);
            _yqzsDAL = new YqzsDAL(_dataContext);
            _trackDAL = new TrackDAL(_dataContext);
            _attachmentDAL = new AttachmentDAL(_dataContext);
            _unitDAL = new UnitDAL();
        }

        public async Task<YqzsCountModel> CountAsync(string deptid, string employeeType)
        {
            var total1 = await _yqzsDAL.Count1Async(deptid, employeeType);
            var total2 = await _yqzsDAL.Count2Async(deptid, employeeType);
            return new YqzsCountModel() { Total1 = total1, Total2 = total2 };
        }

        public async Task<IEnumerable<YqzsModel>> ListAsync()
        {
            var data = await _yqzsDAL.ListAsync();
            return data.Select(x => new YqzsModel() { YqzsId = x.YqzsId, BodyTemperature = x.BodyTemperature, HasTouched = x.HasTouched, Remark = x.Remark, StayWorking = x.StayWorking, SurveyResult = x.SurveyResult, Symptom = x.Symptom });
        }

        public async Task CreateAsync(YqzsModel model)
        {
            var entity = new Yqzs()
            {
                YqzsId = model.YqzsId,
                BodyTemperature = model.BodyTemperature,
                HasTouched = model.HasTouched,
                Remark = model.Remark,
                StayWorking = model.StayWorking,
                SurveyResult = model.SurveyResult,
                Symptom = model.Symptom,
                Tracks = model.Tracks.Select(y => new Track() { TrackId = y.TrackId, ArrivingPlace = y.ArrivingPlace, ArrivingTime = y.ArrivingTime, DeparturePlace = y.DeparturePlace, DepartureTime = y.DepartureTime, TrackCode = y.TrackCode, Trasportation = y.Trasportation }).ToList()
            };
            await _yqzsDAL.CreateAsync(entity);
            await _yqzsDAL.SaveChangesAsync();
        }

        public async Task DeleteAsync(Guid id)
        {
            await _yqzsDAL.DeleteAsync(id);
            await _yqzsDAL.SaveChangesAsync();
        }

        public async Task UpdateAsync(YqzsModel model)
        {
            var entity = await _yqzsDAL.GetAsync(model.YqzsId);
            if (entity == null)
            {
                entity = new Yqzs()
                {
                    YqzsId = model.YqzsId,
                    BodyTemperature = model.BodyTemperature,
                    HasTouched = model.HasTouched,
                    Remark = model.Remark,
                    StayWorking = model.StayWorking,
                    SurveyResult = model.SurveyResult,
                    Symptom = model.Symptom
                };
                await _yqzsDAL.CreateAsync(entity);
            }
            else
            {
                entity.BodyTemperature = model.BodyTemperature;
                entity.HasTouched = model.HasTouched;
                entity.Remark = model.Remark;
                entity.StayWorking = model.StayWorking;
                entity.SurveyResult = model.SurveyResult;
                entity.Symptom = model.Symptom;

                await _yqzsDAL.UpdateAsync(entity);
            }

            var tracks = await _trackDAL.ListAsync(model.YqzsId);
            foreach (var item in model.Tracks)
            {
                if (tracks.Any(x => x.TrackId == item.TrackId))
                {
                    var trackEntity = tracks.First(x => x.TrackId == item.TrackId);
                    trackEntity.ArrivingPlace = item.ArrivingPlace;
                    trackEntity.ArrivingTime = item.ArrivingTime;
                    trackEntity.DeparturePlace = item.DeparturePlace;
                    trackEntity.DepartureTime = item.DepartureTime;
                    trackEntity.TrackCode = item.TrackCode;
                    trackEntity.Trasportation = item.Trasportation;
                    await _trackDAL.UpdateAsync(trackEntity);
                }
                else
                {
                    await _trackDAL.CreateAsync(new Track()
                    {
                        TrackId = item.TrackId,
                        ArrivingPlace = item.ArrivingPlace,
                        ArrivingTime = item.ArrivingTime,
                        DeparturePlace = item.DeparturePlace,
                        DepartureTime = item.DepartureTime,
                        TrackCode = item.TrackCode,
                        Trasportation = item.Trasportation,
                        YqzsId = model.YqzsId
                    });
                }
            }
            var deleteTrasks = tracks.Where(x => !model.Tracks.Any(y => y.TrackId == x.TrackId));
            await _trackDAL.DeleteAsync(deleteTrasks);

            var attachmentEntities = await _attachmentDAL.ListAsync(entity.YqzsId, "yqzs");
            foreach (var item in model.Attachments)
            {
                if (attachmentEntities.Any(x => x.AttachmentId == item.AttachmentId)) continue;

                await _attachmentDAL.CreateAsync(new Attachment()
                {
                    AttachmentId = item.AttachmentId,
                    FileName = item.FileName,
                    FileStream = item.FileStream,
                    Extension = item.Extension,
                    MediaType = item.MediaType,
                    Option = "yqzs",
                    BusinessId = model.YqzsId.ToString()
                });
            }
            var deleteAttachments = attachmentEntities.Where(x => !model.Attachments.Any(y => y.AttachmentId == x.AttachmentId));
            await _attachmentDAL.DeleteAsync(deleteAttachments);

            await _yqzsDAL.SaveChangesAsync();
        }

        //public async Task<YqzsModel> GetAsync(Guid id)
        //{
        //    var employeeEntity = await _employeeDAL.GetAsync(id);
        //    var entity = await _yqzsDAL.GetAsync(id);
        //    var tracks = await _trackDAL.ListAsync(id);
        //    var attachments = await _attachmentDAL.ListAsync(id, "yqzs");
        //    var departmentEntity = await _unitDAL.GetEntity(employeeEntity.DepartmentId);
        //    if (entity == null) return null;
        //    return new YqzsModel()
        //    {
        //        YqzsId = entity.YqzsId,
        //        BodyTemperature = entity.BodyTemperature,
        //        HasTouched = entity.HasTouched,
        //        Remark = entity.Remark,
        //        StayWorking = entity.StayWorking,
        //        SurveyResult = entity.SurveyResult,
        //        Symptom = entity.Symptom,
        //        Tracks = tracks.Select(y => new TrackModel() { TrackId = y.TrackId, ArrivingPlace = y.ArrivingPlace, ArrivingTime = y.ArrivingTime, DeparturePlace = y.DeparturePlace, DepartureTime = y.DepartureTime, TrackCode = y.TrackCode, Trasportation = y.Trasportation }).ToList(),
        //        Employee = new EmployeeModel()
        //        {
        //            EmployeeId = employeeEntity.EmployeeId,
        //            DepartmentId = employeeEntity.DepartmentId,
        //            DisplayName = employeeEntity.DisplayName,
        //            EmployeeNumber = employeeEntity.EmployeeNumber,
        //            EmployeeType = employeeEntity.EmployeeType,
        //            Gender = employeeEntity.Gender,
        //            Hometown = employeeEntity.Hometown,
        //            IdentityNumber = employeeEntity.IdentityNumber,
        //            PhoneNumber = employeeEntity.PhoneNumber,
        //            Post = employeeEntity.Post,
        //            Age = employeeEntity.Age,
        //            ReworkDate = employeeEntity.ReworkDate,
        //            DepartmentName = departmentEntity == null ? null : departmentEntity.UnitName
        //        },
        //        Attachments = attachments.Select(x => new AttachmentModel() { AttachmentId = x.AttachmentId, Extension = x.Extension, FileName = x.FileName, MediaType = x.MediaType }).ToList()
        //    };
        //}
    }
}
